<template>
  <div class="pagination">
    <button :disabled="currentPage === 1" @click="$emit('changePage',currentPage - 1)">上一页</button>
    <button v-if="startEnd.start > 1" @click="$emit('changePage',1)">1</button>
    <button v-if="startEnd.start > 2">···</button>
    <button
      @click="$emit('changePage',page)"
      v-for="page in startEnd.end"
      :key="page"
      v-if="page >= startEnd.start"
      :class="{ active: currentPage === page }"
    >
      {{ page }}
    </button>
    <button v-if="startEnd.end < pageTotal - 1">···</button>
    <button v-if="startEnd.end < pageTotal" @click="$emit('changePage',pageTotal)">{{ pageTotal }}</button>
    <button :disabled="currentPage === pageTotal" @click="$emit('changePage',currentPage + 1)">下一页</button>

    <button style="margin-left: 30px">共 {{ Total }} 条</button>
  </div>
</template>

<script>
export default {
  name: "Pagination",
  props: {
    currentPage: {
      type: Number,
      default: 1,
    },
    Total: {
      type: Number,
      default: 0,
    },
    continueNum: {
      type: Number,
      default: 3,
    },
    pageSize: {
      type: Number,
      default: 10,
    },
  },
  computed: {
    pageTotal() {
      return Math.ceil(this.Total / this.pageSize);
    },
    startEnd() {
      //3,4,5,6,7
      const { currentPage, pageTotal, continueNum } = this;
      let start = 0;
      let end = 0;
      if (continueNum > pageTotal) {
        start = 1;
        end = pageTotal;
      } else {
        start = currentPage - Math.floor(continueNum / 2 - 2);
        end = currentPage + Math.floor(continueNum / 2 + 2);
        if (start < 1) {
          start = 1;
          end = continueNum;
        } else if (end > pageTotal) {
          end = pageTotal;
          start = pageTotal - continueNum + 1;
        }
      }
      return { start, end };
    },
  },
};
</script>

<style lang="less" scoped>
.pagination {
  button {
    margin: 0 5px;
    background-color: #f4f4f5;
    color: #606266;
    outline: none;
    border-radius: 2px;
    padding: 0 4px;
    vertical-align: top;
    display: inline-block;
    font-size: 13px;
    min-width: 35.5px;
    height: 28px;
    line-height: 28px;
    cursor: pointer;
    box-sizing: border-box;
    text-align: center;
    border: 0;

    &[disabled] {
      color: #c0c4cc;
      cursor: not-allowed;
    }

    &.active {
      cursor: not-allowed;
      background-color: #409eff;
      color: #fff;
    }
  }
}
</style>
